import { Body, Controller, Delete, Get, Param, Post, Put } from "@nestjs/common";
import { CreateBookDto, UpdateBookDto } from './book.dto';
import { BookService } from "./book.service";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { Book } from "./book.entity";

@ApiTags("图书")
@Controller('/api')
// 局部拦截器， 只对当前controller有效
// @UseInterceptors(LoggerInterceptor)
export class BookController  {

  constructor(private readonly service: BookService) {
  }

  @ApiOperation({ summary: "获取图书列表" })
  @Get('books')
  async list(): Promise<Book[]> {
    const books = this.service.list();
    return books;
  }

  @ApiOperation({ summary: "创建图书" })
  @Post('book')
  async create(@Body() createBookDto: CreateBookDto) {
    return await this.service.create(createBookDto);
  }

  @ApiOperation({ summary: "获取图书详情" })
  @Get('books/:bid')
  async detail(@Param('bid') bid: number): Promise<string> {
    const book = `${bid} detail`;
    return book;
  }

  @ApiOperation({ summary: "修改图书信息" })
  @Put('books/:bid')
  update(@Param('bid') bid: string, @Body() updateBookDto: UpdateBookDto) {
    return `${bid} update`;
  }

  @ApiOperation({ summary: "移除图书信息" })
  @Delete('books/:bid')
  remove(@Param('bid') bid: string) {
    return `${bid} remove`;
  }
}
